///////////////////////////////////////////////////////////
//  CCreateCheckListView.cpp
//  Implementation of the Class CCreateCheckListView
//  Created on:      21-Sep-2013 9:36:22 AM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "CCreateCheckListView.h"
#include "ControlCommand.h"
#include "CurrentCommand.h"
#include "CItemView.h"
#include "stdafx.h"
#include "IInt.h"

using namespace UI;
using namespace UI::CMD;

CCreateCheckListView::CCreateCheckListView(IWindowDirector* director)
{
  this->m_type = IViewCtrl::CREATECHECKLISTVIEW_T;
  this->m_parrentWnd = director;
  this->m_checkListPage = NULL;

  // call initialize for create all UI.
  Initialize();
}


CCreateCheckListView::~CCreateCheckListView()
{
  RemovePage();
}

UI::IViewCtrl::ViewType CCreateCheckListView::GetType() const 
{
	return this->m_type;
}


void CCreateCheckListView::Initialize()
{
  this->SetAttribute<int>("x1", 0);
  this->SetAttribute<int>("y1", 0);
  this->SetAttribute("width", "100%");
  this->SetAttribute("height", "90%");
  this->SetAttribute("backgroundColour", CColour(COLOUR_PAGE));

  // Create navigationbar and set right button name
  this->m_navigationBar = CreateNavigationBar(CAttributes()
    .Set("name", "navBar")
    .Set("title", "Checklist")
    .Set("rightButtonCaption", "Save")
    .Set("leftButtonCaption", "Back")
    .Set("x1",      0)
    .Set("y1",      0)
    .Set("width",   "100%")
    .Set("height",  "10%"));

  // Setting event handler for handling navigation bar click
  this->m_navigationBar->SetEventHandler("clicknavigationbar", this, &CCreateCheckListView::OnClickedNevigationBar);

  // Add navigation bar in view
  this->AddChild(this->m_navigationBar);

  this->m_table = CreateTable(CAttributes()
    .Set("name",    "TablePages")
    .Set("x1",      0)
    .Set("y1",      "10%")
    .Set("width",   "100%")
    .Set("height",  "90%")
    .Set("alignH",  "top"));

  this->m_table->SetEventHandler("selectrow", this, &CCreateCheckListView::OnSelectedTable);

  // Add table in view
  this->AddChild(this->m_table);

  this->m_alretDlg = CreateAlertDialog(CAttributes()
    .Set("name",                    "AlertDialog")
    .Set("title",                   "Save")
    .Set("message",                 "Would you need to save this checklist?")
    .Set("positiveButtonCaption",   "Yes")
    .Set("negativeButtonCaption",   "No")
    .Set("neutralButtonCaption",    "Cancel"));


  this->m_alretDlg->SetEventHandler("dismissalertdialog", this, &CCreateCheckListView::OnAlertButtonClick);

  /*Refresh();*/

}


void CCreateCheckListView::Refresh()
{
  //// Create scroll view
  //CScrollViewPtr scrollView = CreateScrollView(CAttributes()
  //  .Set("name",    "ScrollView")
  //  .Set("x1",      0)
  //  .Set("y1",      "10%")
  //  .Set("width",   "100%")
  //  .Set("height",  "90%")
  //  .Set("scrollX", 0)
  //  .Set("scrollY", 0)
  //  .Set("direction","vertical")
  //  .Set("backgroundColour", CColour(COLOUR_PAGE)));

  //this->AddChild(scrollView);

  //int childItems = this->m_checkListPage->CountChilds();
  //int iRowCount = 0;

  //for(int itemIndex = 0; itemIndex < childItems; itemIndex++)
  //{
  //  IObject* item = this->m_checkListPage->GetChild(itemIndex);
  //  switch(item->GetType())
  //  {
  //  case IObject::HEARDER_T:
  //    {
  //      IHeader* header = dynamic_cast<IHeader*>(item);
  //      CItemViewPtr itemView = CItemView::CreateItemView(header);
  //      itemView->SetAttribute("x1", 0);
  //      itemView->SetAttribute("y1", iRowCount * 40);
  //      scrollView->AddChild(itemView);
  //      iRowCount++;

  //      // Add child
  //      for(int childIndex = 0; childIndex < header->CountChilds(); childIndex++)
  //      {
  //        IItem* childItem = dynamic_cast<IItem*>(header->GetChild(childIndex));
  //        itemView = CItemView::CreateItemView(childItem, this->m_checkListPage->GetChoices(), this->m_parrentWnd);
  //        itemView->SetAttribute("x1", 0);
  //        itemView->SetAttribute("y1", iRowCount * 40);
  //        scrollView->AddChild(itemView);
  //        iRowCount++;
  //      }
  //    }
  //    break;
  //  case IObject::ITEM_T:
  //    {
  //      CItemViewPtr itemView = CItemView::CreateItemView(dynamic_cast<IItem*>(item), this->m_checkListPage->GetChoices(), this->m_parrentWnd);
  //      itemView->SetAttribute("x1", 0);
  //      itemView->SetAttribute("y1", iRowCount * 40);
  //      scrollView->AddChild(itemView);
  //      iRowCount++;
  //    }
  //    break;
  //  default: break;
  //  }
  //}

  int iRows = (int)this->m_tableRows.size();
  // Remove all row in table
  for(int index = 0; index < iRows; index++)
    this->m_table->RemoveChild(this->m_tableRows[index].second);

  // **** Find the method to delete auto pointer ****
  this->m_tableRows.clear();


  int iChildItems = this->m_checkListPage->CountChilds();
  int iRowCount = 0;

  for(int index = 0; index < iChildItems; index++)
  {
    IObject* item = this->m_checkListPage->GetChild(index);
    switch(item->GetType())
    {
    case IObject::HEARDER_T:
      {
        //char tmp[0xff];
        //snprintf(tmp, 0xff, "HeaderTable%d", index);

        IHeader* header = dynamic_cast<IHeader*>(item);

        //labelText = header->GetName();

        //AddRow(item, tmp, labelText);
        AddTableRow(item);


        iRowCount++;

        // Add child
        int iChildCount = header->CountChilds();

        for(int childIndex = 0; childIndex < iChildCount; childIndex++)
        {
          //snprintf(tmp, 0xff, "ChildItemTable%d", (childIndex + 1) * (index + 1));

          IObject* childItem = header->GetChild(childIndex);

          //labelText = dynamic_cast<IItem*>(childItem)->GetName();
          //labelText = "   - " + labelText;

          AddTableRow(childItem, true);
          //AddRow(childItem, tmp, labelText);

          iRowCount++;
        }
      }
      break;
    case IObject::ITEM_T:
      {
        //char tmp[0xff];
        //snprintf(tmp, 0xff, "ItemTable%d", index);

        //labelText = dynamic_cast<IItem*>(item)->GetName();
        //labelText = "   - " + labelText;

        //AddRow(item, tmp, labelText);
        AddTableRow(item);

        iRowCount++;
      }
      break;
    default: break;
    }

  }
}


void CCreateCheckListView::ShowView(bool show)
{
  this->SetAttribute("visible", show);
}


void CCreateCheckListView::OnClickedSave()
{

}


void CCreateCheckListView::OnClickedBack()
{

}


void CCreateCheckListView::OnClickedChoice()
{

}


void CCreateCheckListView::SetPage(IPage* page)
{
  RemovePage();
  this->m_checkListPage = page;
}


IPage* CCreateCheckListView::GetPage()
{
	return this->m_checkListPage;
}


void CCreateCheckListView::RemovePage()
{
  if(this->m_checkListPage)
    delete this->m_checkListPage;
  this->m_checkListPage = NULL;
}

bool UI::CCreateCheckListView::OnClickedNevigationBar( CNavigationBar *naviBar, int button )
{
  switch(button)
  {
  case S3E_NUI_NAVIGATION_BAR_BUTTON_LEFT: // Click Back
    {
      this->m_alretDlg->Show();
      /*ICommand::CommandType commandType = CurrentCommand::GetInstance()->GetNewOrEditCommand();
      if(commandType == ICommand::NEWCHECKLIST_CMD)
      {
        ControlCommand::GetInstance()->OnBack(ControlCommand::TEMPLATE_CTRL);
        this->m_parrentWnd->OnShowView(this, IViewCtrl::TEMPLATEVIEW_T);
      }
      else if(commandType == ICommand::EDITCHECKLIST_CMD)
      {
        ControlCommand::GetInstance()->OnBack(ControlCommand::CHECKLIST_CTRL);
        this->m_parrentWnd->OnShowView(this, IViewCtrl::CHECKLISTVIEW_T);
      }*/
    }
    break;
  case S3E_NUI_NAVIGATION_BAR_BUTTON_RIGHT: // Click Save
    {
      switch(CurrentCommand::GetInstance()->GetNewOrEditCommand())
      {
      case ICommand::NEWCHECKLIST_CMD:
        {
          this->m_parrentWnd->OnShowView(this, IViewCtrl::SAVEVIEW_T); 
        }
        break;
      case ICommand::EDITCHECKLIST_CMD:
        {
          ControlCommand::GetInstance()->OnSave(ControlCommand::CHECKLIST_CTRL);
          this->m_parrentWnd->OnShowView(this, IViewCtrl::CHECKLISTVIEW_T); 
        }
        break;
      default: break;
      }
    }
    break;
  default: break;
  }
  return true;
}

void UI::CCreateCheckListView::SetTitleView( const std::string& text )
{
  this->m_navigationBar->SetAttribute("title", text.c_str());
}

bool UI::CCreateCheckListView::OnSelectedTable( CTable *table, int tab )
{
  if(tab == -1)
    return true;

  this->m_currentRow = tab;
  IObject* data = this->m_tableRows[tab].first;
  if(data->GetType() == IObject::ITEM_T)
  {
    ControlCommand::GetInstance()->OnEdit(ControlCommand::CHOICE_CTRL);
    this->m_parrentWnd->OnShowView(NULL, IViewCtrl::CHOICESVIEW_T);
  }
  return true;
}

IwNUI::CTableItemPtr UI::CCreateCheckListView::CreateTableRow( IObject* item, bool isChild /*= false*/ )
{
  CTableItemPtr tableItem = CreateTableItem(CAttributes()
    .Set("width",   "100%")
    .Set("height",  "50"));

  
  std::string labelName = "Lable_";

  switch(item->GetType())
  {
  case IObject::ITEM_T:
    {
      CLabelPtr label;
      std::string labelText;
      IItem* pItem = dynamic_cast<IItem*>(item);
      labelText = pItem->GetName();
      labelName += labelText;
      if(isChild)
        labelText = " - " + labelText;
      label = CreateLabel(CAttributes()
        .Set("x1", 0)
        .Set("y1", 0)
        .Set("name",    labelName.c_str())
        .Set("caption", labelText.c_str())
        .Set("width", "50%")
        .Set("height", "100%")
        .Set("fontColour",       CColour(COLOUR_LABEL))
        );
      tableItem->AddChild(label);
      
      label = CreateLabel(CAttributes()
        .Set("name",  "Label")
        .Set("x1", "85%")
        .Set("y1", 0)
        .Set("caption", ">")
        .Set("width", "15%")
        .Set("height", "100%")
        );
      tableItem->AddChild(label);

      labelName = "Choice_" + pItem->GetName();

      IChoices* choices = this->m_checkListPage->GetChoices();

      label = CreateLabel(CAttributes()
        .Set("name",  labelName.c_str())
        .Set("x1", "50%")
        .Set("y1", 0)
        .Set("caption", choices->GetChoice(dynamic_cast<IInt*>(pItem->GetData())->GetValue()).c_str())
        .Set("width", "35%")
        .Set("height", "100%")
        .Set("fontAlignH", "right")
        );
      tableItem->AddChild(label);
    }
    break;
  case IObject::HEARDER_T:
    {
      CLabelPtr label;
      std::string labelText;
      labelText = dynamic_cast<IHeader*>(item)->GetName();
      labelName += labelText;
      label = CreateLabel(CAttributes()
        .Set("name",    labelName.c_str())
        .Set("caption", labelText.c_str())
        .Set("width", "100%")
        .Set("height", "100%")
        .Set("fontStyle",  "bold")
        .Set("fontColour",       CColour(COLOUR_LABEL))
        //.Set("backgroundColour", COLOUR_GREY)
        .Set("shadow",           true)
        .Set("shadowOffset",     CVector2(0, 2))
        .Set("shadowColour",     CColour(COLOUR_WHITE))
        .Set("fontSize",    "large")
        );
      tableItem->AddChild(label);
    }
    break;
  default: break;
  }

  return tableItem;
}

void UI::CCreateCheckListView::AddTableRow( IObject* item, bool isChild /*= false*/ )
{
  CTableItemPtr tableItem = CreateTableRow(item, isChild);

  this->m_table->AddChild(tableItem);

  this->m_tableRows.push_back(std::make_pair(item, tableItem));
}

void UI::CCreateCheckListView::EditData( int newChoice )
{
  IObject* item = this->m_tableRows[this->m_currentRow].first;
  dynamic_cast<IInt*>(dynamic_cast<IItem*>(item)->GetData())->SetValue(newChoice);
  CTableItemPtr itemRow = this->m_tableRows[this->m_currentRow].second;
  IChoices* choices = this->m_checkListPage->GetChoices();
  itemRow->GetChild(2)->SetAttribute("caption", choices->GetChoice(newChoice).c_str());
}

int UI::CCreateCheckListView::GetCurChoice()
{
  IObject* item = this->m_tableRows[this->m_currentRow].first;
  return dynamic_cast<IInt*>(dynamic_cast<IItem*>(item)->GetData())->GetValue();
}

bool UI::CCreateCheckListView::OnAlertButtonClick( CAlertDialog* dialog, EAlertDialogButton button )
{
  switch(button)
  {
  case IWNUI_ALERT_DIALOG_POSITIVE:
    {
      switch(CurrentCommand::GetInstance()->GetNewOrEditCommand())
      {
      case ICommand::NEWCHECKLIST_CMD:
        {
          this->m_parrentWnd->OnShowView(this, IViewCtrl::SAVEVIEW_T); 
        }
        break;
      case ICommand::EDITCHECKLIST_CMD:
        {
          ControlCommand::GetInstance()->OnSave(ControlCommand::CHECKLIST_CTRL);
          this->m_parrentWnd->OnShowView(this, IViewCtrl::CHECKLISTVIEW_T); 
        }
        break;
      default: break;
      }
    }
    break;
  case IWNUI_ALERT_DIALOG_NEGATIVE:
    {
      ICommand::CommandType commandType = CurrentCommand::GetInstance()->GetNewOrEditCommand();
      if(commandType == ICommand::NEWCHECKLIST_CMD)
      {
        ControlCommand::GetInstance()->OnBack(ControlCommand::TEMPLATE_CTRL);
        this->m_parrentWnd->OnShowView(this, IViewCtrl::TEMPLATEVIEW_T);
      }
      else if(commandType == ICommand::EDITCHECKLIST_CMD)
      {
        ControlCommand::GetInstance()->OnBack(ControlCommand::CHECKLIST_CTRL);
        this->m_parrentWnd->OnShowView(this, IViewCtrl::CHECKLISTVIEW_T);
      }
    }
    break;
  case IWNUI_ALERT_DIALOG_NEUTRAL:
    break;
  default: break;
  }
  return true;
}
